//
//  TopViewOfScrollView.m
//  aMoment
//
//  Created by dllo on 15/10/13.
//  Copyright © 2015年 LuckyHan. All rights reserved.
//

#import "TopViewOfScrollView.h"
#import "AllHeaderFiles.h"

@implementation TopViewOfScrollView

- (void)dealloc
{
    [_scrollView release];
    [_carouselArray release];
    [_timer release];
    [super dealloc];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.scrollView = [[UIScrollView alloc] init];
        _scrollView.pagingEnabled = YES;
        _scrollView.delegate = self;
        _scrollView.showsHorizontalScrollIndicator = NO;
        [self addSubview:_scrollView];
        [_scrollView release];
    }
    return self;
}

#pragma mark 重写set
- (void)setCarouselArray:(NSArray *)carouselArray
{
    if (_carouselArray != carouselArray) {
        [_carouselArray release];
        _carouselArray = [carouselArray retain];
    }
    [self setScrollViewWithCarouselArray:_carouselArray];
}

#pragma mark 创建视图
- (void)setScrollViewWithCarouselArray:(NSArray *)carouselArray
{
    _scrollView.frame = CGRectMake(0, 0, self.Width, self.Height);
    _scrollView.contentOffset = CGPointMake(_scrollView.Width, 0);
    _scrollView.contentSize = CGSizeMake(_scrollView.Width * (carouselArray.count + 2), _scrollView.Height);
    NSString *imgURLString = [NSString string];
    for (NSInteger i = 0; i <= carouselArray.count + 1; i++) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(_scrollView.Width * i, 0, _scrollView.Width, _scrollView.Height)];
        imageView.contentMode = UIViewContentModeScaleAspectFill;
        imageView.clipsToBounds = YES;
        if (i == 0) {
            imgURLString = [carouselArray.lastObject objectForKey:@"img"];
            imageView.tag = 1000;
        }
        else if (i == carouselArray.count + 1) {
            imgURLString = [carouselArray.firstObject objectForKey:@"img"];
            imageView.tag = carouselArray.count - 1 + 1000;
        }
        else {
            imgURLString = [carouselArray[i - 1] objectForKey:@"img"];
            imageView.tag = i - 1 + 1000;
        }
//        [imageView setImageWithURL:[NSURL URLWithString:imgURLString]];
        [imageView setImageWithURL:[NSURL URLWithString:imgURLString] placeholderImage:[UIImage imageNamed:@"placeholder-image"]];
        [_scrollView addSubview:imageView];
        [imageView release];
    }
    _timer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(autoScroll) userInfo:nil repeats:YES];
}

#pragma mark 定时器自动滚动
- (void)autoScroll
{
    [UIView animateWithDuration:.5 animations:^{
        _scrollView.contentOffset = CGPointMake(_scrollView.contentOffset.x + _scrollView.Width, 0);
    } completion:^(BOOL finished) {
        if (_scrollView.contentOffset.x / _scrollView.Width > _carouselArray.count + .5) {
            _scrollView.contentOffset = CGPointMake(_scrollView.Width, 0);
        }
    }];
}

#pragma mark 将要开始拖拽
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    //关闭计时器
    [_timer setFireDate:[NSDate distantFuture]];
    
    CGFloat offsetX = scrollView.contentOffset.x / scrollView.Width;
    if (offsetX > _carouselArray.count + .5) {
        scrollView.contentOffset = CGPointMake(scrollView.Width, 0);
    }
    if (offsetX < .5) {
        scrollView.contentOffset = CGPointMake(scrollView.Width * _carouselArray.count, 0);
    }
}

#pragma mark 结束减速
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (scrollView.contentOffset.x / scrollView.Width > _carouselArray.count + .5) {
        scrollView.contentOffset = CGPointMake(scrollView.Width, 0);
    }
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        if (!scrollView.dragging) {
            //再次打开计时器
            [_timer setFireDate:[NSDate distantPast]];
        }
    });
}

#pragma mark 图片点击方法
- (void)clickImageAction
{
    
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
